/*
Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package GuiTools;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import Settings.*;

/**
 * @author Christoph Schnack
 *
 */
public class WordList extends JPanel {
	JList lpList;
	JLabel lpListSizeLabel,lpListTitleLabel;
	TribuitTextField lpField;
	boolean lp_inverse;
	String[] l1Array,l2Array;
	public JRadioButton lpLang1RB,lpLang2RB;
	String lang1,lang2;
	Settings SET;
	Language LANG;
	
	public WordList(Settings set,String title,String l1, String l2,String[] arg_array,String[] arg_array_inverse){
		super();
		SET=set;
		LANG=set.getLanguage();
		l1Array = arg_array;
		l2Array = arg_array_inverse;
		lang1=l1;
		lang2=l2;
		lp_inverse = false;
		//Liste
		lpList = new JList();
		lpList.addMouseListener(new MouseAdapter() {
    	     public void mouseClicked(MouseEvent e) {
    	    	 if(e.getClickCount()==2)mouseClick();
    	     }
		});
		JScrollPane lpJSP = new JScrollPane(lpList);
		lpJSP.setPreferredSize(new Dimension(300,250));
		lpList.setListData(arg_array);
		lpList.setCellRenderer(new evdListRenderer());
	//TextField
		lpField = new TribuitTextField(SET,20);
		lpField.addKeyListener(new lpSearchTFKeyListener());
	//InfoPanel
		JPanel infoPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		lpListSizeLabel = new JLabel();
		lpListTitleLabel = new JLabel(title);
		lpListSizeLabel.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,lpListSizeLabel.getFont().getSize()));
		lpListTitleLabel.setFont(new Font(Font.SANS_SERIF,Font.PLAIN,lpListSizeLabel.getFont().getSize()));
		lpListTitleLabel.setPreferredSize(new Dimension(180,lpListTitleLabel.getPreferredSize().height));
		//infoPanel.setBackground(sizeLP.getBackground());
		infoPanel.add(new JLabel(LANG.getWords()+": "));
		infoPanel.add(lpListSizeLabel);
		infoPanel.add(new JLabel(LANG.getContent()+": "));
		infoPanel.add(lpListTitleLabel);
		countWords();
	//RadioButtons
		ButtonGroup lpBg = new ButtonGroup();
		lpLang1RB = new JRadioButton(lang1,true);
		lpLang1RB.setPreferredSize(new Dimension(120,26));
		lpLang1RB.addActionListener(new lpLangListener());
		lpBg.add(lpLang1RB);
		lpLang2RB = new JRadioButton(lang2);
		lpLang2RB.setPreferredSize(new Dimension(120,26));
		lpLang2RB.addActionListener(new lpLangListener());
		lpBg.add(lpLang2RB);
		JPanel lpLangRBPanel = new JPanel();
		lpLangRBPanel.setLayout(new GridLayout(1,2));
		lpLangRBPanel.add(lpLang1RB);
		lpLangRBPanel.add(lpLang2RB);
		lpLangRBPanel.setOpaque(false);
		lpLang1RB.setOpaque(false);
		lpLang2RB.setOpaque(false);
		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		this.add(new LabelPanel(LANG.getSearch()+":",lpField));
		this.add(lpJSP);
		this.add(lpLangRBPanel);
		this.add(infoPanel);
	}
	
	public void setVocabularyNames(String a, String b){
		lang1 = a;
		lang2 = b;
		lpLang1RB.setText(a);
		lpLang2RB.setText(b);
	}
	
	public void clickLangABtn(){
		lpLang1RB.doClick();
	}
	
	public void clickLangBBtn(){
		lpLang2RB.doClick();
	}
	
	class lpLangListener implements ActionListener{
    	public void actionPerformed(ActionEvent e){
    		lpList.removeAll();
    		JRadioButton choice = (JRadioButton) e.getSource();
    		if(choice.getText().equals(lang2)){
    			if(l2Array!=null){
    				lpList.setListData(l2Array);
    				lp_inverse = true;
    			}
    		}
    		else{
    			if(l1Array!=null){
    				lpList.setListData(l1Array);
    				lp_inverse = false;
    			}
    		}
    	}
    }
	
	public void tfGrabFocus(){
		lpField.grabFocus();
	}
	
	public boolean isInverse(){
		return lp_inverse;
	}
	
	public void setSelectedIndex(int arg){
		if(lpList.getModel().getSize()>0)lpList.setSelectedIndex(arg);
	}
	
	public void grabFocusList(){
		lpList.grabFocus();
	}
	
	public boolean isSelectionEmpty(){
		return lpList.isSelectionEmpty();
	}
	
	public Object[] getSelectedValues(){
		return lpList.getSelectedValues();
	}
	
	public String getSelectedValue(){
		return (String) lpList.getSelectedValue();
	}
	
	public void removeSelectionFromList(){
		lpList.getModel().getElementAt(lpList.getSelectedIndex());
		lpList.validate();
	}
	
	public void setListPanelData(String name,String[] words, String[] wordsInverse){
		l1Array=words;
		l2Array=wordsInverse;
		countWords();
		if(l1Array!=null){
			lpListTitleLabel.setText(name);
			if(lpLang1RB.isSelected()) lpLang1RB.doClick();
			else lpLang2RB.doClick();
		}
	}
	
	public void countWords(){
		int counter = 0;
		if(l1Array!=null){
    		for(String str : l1Array){
    			if(str.contains("-")) counter++;
    		}
		}
		lpListSizeLabel.setText(counter+"  ");
	}
	
	class lpSearchTFKeyListener implements KeyListener{
    	public void keyTyped(KeyEvent e){}
    	public void keyReleased(KeyEvent e){
    		String word = lpField.getText().toLowerCase();
    		if(!word.equals("")) lpSearchInList(word);
    	}
    	public void keyPressed(KeyEvent e){}
	
    	public void lpSearchInList(String pattern){
	    	int l = pattern.length();
	    	ListModel lm = lpList.getModel();
	    	if(lm.getSize()>0){
		    	for(int i=0;i<lm.getSize();i++){
		    		String st = (String) lm.getElementAt(i);
		    		String[] split = st.split(" - ");
		    		String sub;
		    		if(split[0].length()<=l) sub = split[0];
		    		else sub = split[0].substring(0,l);
		    		pattern = pattern.toLowerCase();
		    		sub = sub.toLowerCase();
		    		if(pattern.equals(sub)){
		    			lpList.setSelectedIndex(i);
		    			lpList.ensureIndexIsVisible(i);
		    			break;
		    		}
		    	}
	    	}
    	}
	}
	
	class evdListRenderer extends JLabel implements ListCellRenderer {
   	 
        private Font extraFont;
        private Font normalFont;
     
        public evdListRenderer() {
            setOpaque(true);
            normalFont = new Font(Font.SERIF,Font.PLAIN,15);
            extraFont = new Font(Font.SERIF,Font.BOLD,17);
        }
     
        public Component getListCellRendererComponent(
                final JList list,
                final Object value,
                final int index,
                final boolean isSelected,
                final boolean cellHasFocus) {
        	if(value!=null){
        		String tmp = value.toString();
        		setText(tmp);
        		if (tmp.length()>1) setFont(normalFont);
	            else setFont(extraFont);
        	}
            if (isSelected) {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            } else {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }
            return this;
        }
    }
	
	public void mouseClick(){}
}
